package com.huatai.common.config;

import com.github.xiaoymin.knife4j.spring.extension.OpenApiExtensionResolver;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

/**
 * @program: ht-system
 * @description: 文档配置
 * @author: songxiude
 * @create: 2023-02-01 15:17
 **/
@Data
@Configuration
@EnableSwagger2WebMvc
@ConfigurationProperties(prefix="swagger")
public class Knife4jConfiguration {

	/*引入Knife4j提供的扩展类*/
	private final OpenApiExtensionResolver openApiExtensionResolver;

    private String version;

    private String termsOfServiceUrl;

    private String description;

    private String basePackage;

	@Autowired
	public Knife4jConfiguration(OpenApiExtensionResolver openApiExtensionResolver) {
		this.openApiExtensionResolver = openApiExtensionResolver;
	}

	@Bean(value = "defaultApi2")
	public Docket defaultApi2() {


		Docket docket = new Docket(DocumentationType.SWAGGER_2)
			.apiInfo(new ApiInfoBuilder()
				.description(description)
				.termsOfServiceUrl(termsOfServiceUrl)
				.version(version)
				.build())
			.select()
			//这里指定Controller扫描包路径
			.apis(RequestHandlerSelectors.basePackage(basePackage))
			.paths(PathSelectors.any())
			.build()
			.extensions(openApiExtensionResolver.buildExtensions(description));
        return docket;
    }
}
